package cn.sdormitory.controller.common;

import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.common.utils.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Api(tags = "common-file => 公共接口")
@RequestMapping("/common/file")
public class CommonController {
    @Value("${file.upload.path}")
    private String fileFolder;

    @ApiOperation(value = "上传文件")
    @PostMapping("/uploadFile")
    public ResultMsg uploadFile(@RequestParam("file") MultipartFile file){
        String filename = null;
        if(file.getSize()>1024 * 1024){
            return  ResultMsg.BY_FAIL("上传文件请控制在1M以内");
        }
        try{
            filename = FileUtil.uploadFile(file, fileFolder);
        }catch(Exception e){
            e.printStackTrace();
        }
        return  ResultMsg.BY_SUCCESS("上传成功",filename);
    }
}
